<?php


namespace app\api\controller;

use app\data\model\Loans;
use app\data\model\LoansInstitutions;
use app\data\model\LoansInstitutionTypes;
use app\data\model\LoansMethod;
use think\admin\Controller;

class Loan extends Controller
{
    public function index()
    {
        $page           = input('page', 1);
        $limit          = input('limit', 10);
        $typeId         = input('loans_institution_type_id', null);
        $termId         = input('loans_term_id', null);
        $methodId       = input('loans_method_id', null);
        $quotaId        = input('loans_quota_id', null);
        $institutionId  = input('loans_institution_id', null);
        $applyNum       = input('apply_num', null);
        $simpleInterest = input('simple_interest', null);
        $list           = Loans::query()->with(['institutionType'])
            ->where('status', '=', 1)
            ->where('deleted', '=', 0);
        // 机构类型
        if ($typeId !== '' && !is_null($typeId)) {
            $list = $list->where('institution_type', '=', $typeId);
        }
        //贷款期限
        if ($termId !== '' && !is_null($termId)) {
            $term = Loans::getLoanTermInfo($termId);
            if ($term) {
                $list = $list->where('loan_term_end', '<=', $term['end'] ?? 0);
            }
        }
        //贷款方式
        if ($methodId !== '' && !is_null($methodId)) {
            $list = $list->whereFindInSet('method', $methodId);
        }
        //贷款额度
        if ($quotaId !== '' && !is_null($quotaId)) {
            $quota = Loans::getLoanQuotaInfo($quotaId);
            $list  = $list->where('loan_term_end', '<=', $quota['end']);
        }
        //金融机构
        if ($institutionId !== '' && !is_null($institutionId)) {
            $list = $list->where('institution', '=', $typeId);
        }
        //贷款利率排序
        if ($simpleInterest) {
            $orderBy = $simpleInterest === 'desc' ? 'desc' : 'asc';
            $field   = $orderBy === 'desc' ? 'simple_interest_end' : 'simple_interest_start';
            $list    = $list->order($field, $orderBy);
        }
        //申请数量排序
        if ($applyNum) {
            $orderBy = $applyNum === 'desc' ? 'desc' : 'asc';
            $list    = $list->order('apply_num', $orderBy);
        } else {
            $list = $list->order('id', 'desc');
        }

        $list = $list->paginate(['list_rows' => $limit ?: 15, 'page' => $page ?: 1]);
        foreach ($list->items() as $item) {
            /**
             * @var Loans $item
             */
            $item->setAttr('institution_type_name', $item->institutionType->institutions_type_name ?? '-');
            $item->setAttr('services_process_list', $item->getServicesProcessList());
            $item->setAttr('method_list', $item->getMethodList());
            $item->setAttr('institution_name', LoansInstitutions::query()->where('id', '=', $item->institution)->value('institutions_name'));
            unset($item->institutionType);
        }

        return_msg(200, 'success', [
            'list'  => $list->items(),
            'page'  => $list->currentPage(),
            'total' => $list->total(),
        ]);
    }

    public function searchParams()
    {
        return_msg(200, 'success', [
            //机构类型
            'loans_institution_types' => LoansInstitutionTypes::query()->field(['id', 'institutions_type_name'])
                ->where('status', '=', 1)->where('deleted', '=', 0)
                ->select(),
            //贷款期限
            'loan_term'               => Loans::LOAN_TERM,
            //贷款担保方式
            'loans_method'            => LoansMethod::query()->field(['id', 'method_name'])
                ->where('status', '=', 1)->where('deleted', '=', 0)
                ->select(),
            //贷款额度
            'loan_quota'              => Loans::LOAN_QUOTA,
            //金融机构
            'loans_institutions'      => LoansInstitutions::query()->field(['id', 'institutions_name'])
                ->where('status', '=', 1)->where('deleted', '=', 0)
                ->select(),
        ]);
    }

    public function detail()
    {
        $id   = input('loan_id');
        $item = Loans::query()->with(['institutionType'])
            ->where('id', '=', $id)->find();
        if (!$item) {
            return_msg(404, '数据不存在');
        }
        $item->setAttr('institution_type_name', $item->institutionType->institutions_type_name ?? '-');
        $item->setAttr('services_process_list', $item->getServicesProcessList());
        $arr = $item->getMethodList()->toArray();
        $methodList = array_column($arr, 'method_name');
        $methodText = implode('、', $methodList);
        $item->setAttr('method_text', $methodText);
        $item->setAttr('method_list', $methodList);
        $item->setAttr('institution_name', LoansInstitutions::query()->where('id', '=', $item->institution)->value('institutions_name'));
        $img1 = '/storage/21729580651_.pic.jpg';
        $img2 = '/storage/31729580651_.pic.jpg';
        $img3 = '/storage/41729580651_.pic.jpg';
        $item->setAttr('quota_img', build_img_url($img1));
        $item->setAttr('period_img', build_img_url($img2));
        $item->setAttr('simple_img', build_img_url($img3));
        unset($item->institutionType);
        return_msg(200, 'success', $item);
    }
}
